package cn.iocoder.yudao.module.train.service.studentcoursescore;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.train.dal.dataobject.connectDTO.StudentCourseScoreStudentDTO;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.train.controller.admin.studentcoursescore.vo.*;
import cn.iocoder.yudao.module.train.dal.dataobject.studentcoursescore.StudentCourseScoreDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.train.dal.mysql.studentcoursescore.StudentCourseScoreMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.train.enums.ErrorCodeConstants.*;

/**
 * 学生课程成绩 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class StudentCourseScoreServiceImpl implements StudentCourseScoreService {

    @Resource
    private StudentCourseScoreMapper studentCourseScoreMapper;

    @Override
    public Integer createStudentCourseScore(StudentCourseScoreSaveReqVO createReqVO) {
        // 插入
        StudentCourseScoreDO studentCourseScore = BeanUtils.toBean(createReqVO, StudentCourseScoreDO.class);
        studentCourseScoreMapper.insert(studentCourseScore);

        // 返回
        return studentCourseScore.getId();
    }

    @Override
    public void updateStudentCourseScore(StudentCourseScoreSaveReqVO updateReqVO) {
        // 校验存在
        validateStudentCourseScoreExists(updateReqVO.getId());
        // 更新
        StudentCourseScoreDO updateObj = BeanUtils.toBean(updateReqVO, StudentCourseScoreDO.class);
        studentCourseScoreMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentCourseScore(Integer id) {
        // 校验存在
        validateStudentCourseScoreExists(id);
        // 删除
        studentCourseScoreMapper.deleteById(id);
    }

    @Override
        public void deleteStudentCourseScoreListByIds(List<Integer> ids) {
        // 删除
        studentCourseScoreMapper.deleteByIds(ids);
        }


    private void validateStudentCourseScoreExists(Integer id) {
        if (studentCourseScoreMapper.selectById(id) == null) {
            throw exception(STUDENT_COURSE_SCORE_NOT_EXISTS);
        }
    }

    @Override
    public StudentCourseScoreStudentDTO getStudentCourseScore(Integer id) {
        return studentCourseScoreMapper.selectById(id);
    }

    @Override
    public PageResult<StudentCourseScoreStudentDTO> getStudentCourseScorePage(StudentCourseScorePageReqVO pageReqVO) {
        return studentCourseScoreMapper.selectPage(pageReqVO);
    }

}
